<?php

// Prevent much happening if this script is called directly
if ( !defined('INCS') ) exit;

// Mail

class Mail
{
    // Singleton Class - cannot be cloned and can only be instantiated
    // through getInstance().
    private function __clone(){}
    private function __construct(){}
    public static function getInstance( $config )
    {
        if (!self::$m_pInstance)
        {
            self::$m_pInstance = new Mail();

            self::$m_pInstance->config = $config;
        }

        return self::$m_pInstance;
    }

    // Private Properties
    private static $m_pInstance;
    private $config;

    public function send( $form )
    {
        $e = self::$m_pInstance;

        $testmode_address = $e->config['email_test_mode'];
        if ( $testmode_address !== '' ) $to = $testmode_address;
        else $to = $e->config['email_recipients'][$form->recipient];

        $send_from = $e->config['sendmail_from'];

        $additional_headers = "From: website contact <$send_from>\nReturn-Path: $send_from";

        $subject = $e->config['subject'];

        $body;
        $body[] = 'To: ';
        $body[] = $to;
        $body[] = "\r\n\r\nFrom: ";
        $body[] = $form->visitor_name;
        $body[] = "\r\nEmail: ";
        $body[] = $form->visitor_email;
        $body[] = "\r\n\r\nDate: ";
        $body[] = date('l, j \of F Y \a\t H:i P');
        $body[] = "\r\n\r\nTheir message:\r\n\r\n";
        $body[] = $form->visitor_message;
        $message = implode($body);

        // TODO Log this submission email namelen, bodylen

        return mb_send_mail( $to, $subject, $message, $additional_headers );
    }
}
